<template>
  <div class="article-page">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <!-- 头部内容 -->
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="showDrawer('add')"
            type="primary"
            size="small"
            round
            icon="el-icon-plus"
            >添加面经</el-button
          >
        </div>
      </template>
      <!-- 表格 -->
      <!-- data这个属性是绑定表格要渲染的数组 -->
      <el-table :data="list" style="width: 100%">
        <!--
          label控制列名（表头的列名）
          prop要跟数据的属性名保持一致，因为它是用来控制这一列什么属性的数据
         -->
        <el-table-column prop="stem" label="标题" width="260">
        </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template>
            <div class="actions">
              <i @click="showDrawer('preview')" class="el-icon-view"></i>
              <i @click="showDrawer('edit')" class="el-icon-edit-outline"></i>
              <i class="el-icon-delete"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!--
        page-size：设置每页的页容量，要跟每次查询出的数据容量保持一致
        total: 设置总数量
        这个插件会自动根据总数量和页容量算出总页数

        current-change事件是当页码发生改变调用
       -->
      <el-pagination
        @current-change="pageChange"
        :page-size="pageSize"
        background
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!--
      title：设置标题
      visible：控制显示和隐藏
      before-close：关闭前的回调
      行内属性不加冒号，那么都是相当于给的字符串
     -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      :size="600"
    >
      <el-form ref="form" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          富文本编辑器
        </el-form-item>
        <el-form-item>
          <el-button type="primary">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { articleListAPI } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      // 总数
      total: 0,
      // 页容量
      pageSize: 5,
      // 页码
      page: 1,
      // 控制抽屉的显示和隐藏
      drawer: false,
      // 用来标记抽屉打开是什么状态
      type: 'add'
    }
  },
  created () {
    // 调用加载数据的方法
    this.loadData()
  },
  methods: {
    showDrawer (type) {
      // 记录操作
      this.type = type
      // 显示抽屉
      this.drawer = true
    },
    // 抽屉关闭前的回调函数
    handleClose () {
      this.drawer = false
    },
    // 参数就是改变后的最新页码
    pageChange (page) {
      this.page = page
      this.loadData()
    },
    // 用来加载数据的方法
    async loadData () {
      const res = await articleListAPI({
        current: this.page,
        pageSize: this.pageSize
      })
      // 保存数组
      this.list = res.data.rows
      // 保存总数量
      this.total = res.data.total
    }
  },

  computed: {
    title () {
      if (this.type === 'add') {
        return '新增面经'
      } else if (this.type === 'edit') {
        return '修改面经'
      } else {
        return '预览面经'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
